package com.jumi.microservice.common.utils;

import com.jumi.microservice.common.constant.ImageResizeEnum;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author Dirk
 * @Date 2020-11-27 15:17
 */
public class ImageResizeUtil {

    /**
     * oss 图片URL拼接压缩参数
     *
     * @param imageOssUrl
     * @param imageResizeEnum
     * @return
     */
    public static String imageResize(String imageOssUrl, ImageResizeEnum imageResizeEnum) {
        if (ObjectUtils.isEmpty(imageOssUrl)) {
            return "";
        }
        return imageOssUrl.concat(imageResizeEnum.getMessage());
    }

    /**
     * oss 多个图片拼接的URL 处理
     * @param imageOssUrl
     * @param imageResizeEnum
     * @return
     */
    public static String batchImageResize(String imageOssUrl, ImageResizeEnum imageResizeEnum) {
        if (ObjectUtils.isEmpty(imageOssUrl)) {
            return "";
        }
        //记录一下分隔符
        String delimiter = delimiter(imageOssUrl);
        return Arrays.asList(imageOssUrl.split("[，,；;||]")).stream().map(s->s.concat(imageResizeEnum.getMessage()))
                .collect(Collectors.joining(delimiter));
    }

    /**
     * 取出分隔符
     * @param imageOssUrl
     * @return
     */
    public static String delimiter(String imageOssUrl){
        if(imageOssUrl.contains("；")){
            return "；";
        }else if(imageOssUrl.contains(";")){
            return ";";
        }else if(imageOssUrl.contains("，")){
            return "，";
        }else if(imageOssUrl.contains("|")){
            return "|";
        }else if(imageOssUrl.contains("|")){
            return "|";
        }else{
            return ",";
        }
    }

}
